package com.uit.ecommerce.weatherservice.servlets;

import com.uit.ecommerce.dto.AreaDTO;
import com.uit.ecommerce.dto.CityDTO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ngloc_it
 */
@WebServlet(name="HomePage", urlPatterns={"/HomePage"})
public class HomePage extends HttpServlet {
   

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();

//        String function = (String)req.getParameter("function");
//        String area = (String) req.getParameter("area");
//        if((function != null) && (area != null)){
//            if(function.equalsIgnoreCase("loadareas")){
//                try {
//                    ArrayList<AreaDTO> listArea = getAreas();
//                    if(listArea != null){
//                        session.setAttribute("listareas", listArea);
//                        String areaIdCurrent = listArea.get(0).getId();
//                        ArrayList<CityDTO> listCityByArea = getCitiesByAreaId(areaIdCurrent);
//                        if(listCityByArea != null)
//                            session.setAttribute("listcitybyarea", listCityByArea);
//                    }
//
//                    ArrayList<String> listCity = GetListCity();
//                    if(listCity != null){
//                        session.setAttribute("listcity", listCity);
//                    }
//                } catch (Exception ex) {
//                    Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }else if(function.equalsIgnoreCase("loadcity")){
//                try {
//                    String areaNameCurrent = req.getParameter("area");
//                    session.removeAttribute("listcitybyarea");
//                    if (areaNameCurrent != null) {
//                        UpdateCity(areaNameCurrent, resp);
//                        return;
//                    }
//                }catch (Exception ex) {
//                    Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }
//        }

        String path = "./jsps/HomePage.jsp";
        resp.sendRedirect(path);
    }

    private ArrayList<AreaDTO> getAreas() throws Exception{
//        AreaBO areaBo = new AreaBO();
//        return areaBo.getAllArea();
        return null;
    }

    private ArrayList<CityDTO> getCitiesByAreaName(String areaName) throws Exception{
//        AreaBO areaBo = new AreaBO();
//        CityBO cityBo = new CityBO();
//        AreaDTO areaDto = areaBo.getAreaByName(areaName);
//        if(areaDto != null){
//            return cityBo.GetAllCityByAreaCode(areaDto.getId());
//        }
        return null;
    }

    private ArrayList<CityDTO> getCitiesByAreaId(String areaId) throws Exception{
//        CityBO cityBo = new CityBO();
//        return cityBo.GetAllCityByAreaCode(areaId);
        return null;
    }

    private void ProcessExecute(){

    }

    private void UpdateCity(String areaNameCurrent, HttpServletResponse resp) throws Exception {
//        CityBO cityBo = new CityBO();
//        AreaDTO areaDto = new AreaBO().getAreaByName(areaNameCurrent);
//        ArrayList<CityDTO> listCity = cityBo.GetAllCityByAreaCode(areaDto.getId());
//        if(listCity != null){
//            PrintWriter out = resp.getWriter();
//            int n = listCity.size();
//            out.append("<select>");
//            for(int i = 0; i < n; i++){
//                out.println("<option>" + listCity.get(i).getName() + "</option>");
//            }
//            out.append("</select>");
//        }
    }

    private ArrayList<String> GetListCity() throws Exception{
//        CityBO cityBo = new CityBO();
//        WeatherCityBO wCityBo = new WeatherCityBO();
//
//        ArrayList<CityDTO> citiesDto =  cityBo.GetAllCity();
//        if(citiesDto != null){
//            ArrayList<String> citiesStr = new ArrayList<String>();
//            int n = citiesDto.size();
//            for(int i = 0; i < n; i++){
//                citiesStr.add(citiesDto.get(i).getName());
//                //citiesStr.add(wCityBo.get);
//            }
//            return citiesStr;
//        }
        return null;
    }
}
